local BaseWnd = require(SCRIPTS_DIR .. ".wnds.BaseWnd");
local Click = require(SCRIPTS_DIR .. ".uikits.Click");

local GridCell = class("GridCell",function()
    return display.newNode();
end);

function GridCell:ctor()
    local bg = display.newSprite(mgrs.res:getPngPath("bg_grid"));
    local size = bg:getContentSize();
    self:setContentSize(size);
    bg:setPosition(cc.p(size.width/2,size.height/2));
    self:addChild(bg);
    self:setTouchEnabled(false);
    --[[
    local click = Click.new({targetNode = bg});
    click:clickHandler(function()
        print(1111);
    end);
    --]]
end

local ListCell = class("ListCell",function()
    return display.newNode();
end);

function ListCell:ctor(args)
    local sprite = cc.Sprite:create(mgrs.res:getPngPath("putong1"));
    self.s = cc.Sprite:create(mgrs.res:getPngPath("putong2"));
    self:addChild(sprite);
    self:addChild(self.s);
    self:onSelect(false);
    self.nodeSize = sprite:getContentSize();

    local txt = cc.ui.UILabel.new({UILabelType = 2,text = args});
    self:addChild(txt);
end
function ListCell:onSelect(s)
    self.s:setVisible(s);
end


local BackpackWnd = class("BackpackWnd",BaseWnd);
function BackpackWnd:ctor()
	BackpackWnd.super.ctor(self);
end
function BackpackWnd:onOpen()
    self:initPageView();
    self:initListView();
    self:setupCloseBtn("right.close");
end
function BackpackWnd:initPageView()
    local pageview = self:createPageView(
    {
        pageview = self:widget("right.pv"),
        col = 4,
        row = 5,
        columnSpace = 2,
        rowSpace = 2,
        bCirc = true,
        autoMark = true,
        markParent = self:widget("right.mark")
    });

    for i=1,140 do
        pageview:addContent(GridCell.new());
    end
    pageview:setPageChangedCbk(function(event) 
        
    end);
    pageview:setItemClickedCbk(function(event)
        
    end)
    pageview:reload();
    pageview:gotoPage(3,true,true);
    --[[
    self.PageViewMark = self:createPageViewMark({count = pageview:getPageCount()});
    self:widget("right.mark"):addChild(self.PageViewMark);
    self.PageViewMark:setMarkClickCbk(function(index)
        pageview:gotoPage(index,true,true);
    end)
    --]]
end

function BackpackWnd:initListView()
    local listView = self:widget("left.listView");
    local list = self:createListView({listView = listView});
    for i=1,20 do
        list:addContent(ListCell.new(i));
    end
    list:setSelectedCallBack(function(idx)
        mgrs.log:printDebug("clicked = %d",idx);
    end)

    list:reload();
    local num = 1;
    local testBtnUP = self:widget("left.testBtnUP");
    testBtnUP:onButtonClicked(function()
        if num >= #list:getAllItems() then
            return
        end
        testBtnUP:setTouchEnabled(false);
        num = num + 1;
        list:gotoList({index = num,cbk = function()
            testBtnUP:setTouchEnabled(true);
            list:setSelectedByIndex(num)
        end});
    end)
    local testBtnDown = self:widget("left.testBtnDown");
    testBtnDown:onButtonClicked(function()
        if num <= 1 then
            return
        end
        testBtnDown:setTouchEnabled(false);
        num = num - 1;
        list:gotoList({index = num,cbk = function()
            testBtnDown:setTouchEnabled(true);
            list:setSelectedByIndex(num);
        end});
    end)
    

    local testBtnDel = self:widget("left.testBtnDel");
    testBtnDel:onButtonClicked(function()
        list:removeItem();
    end)
end

--[[

function BackpackWnd:touchListener(event)
    local pageView = event.pageView
    if event.name == "clicked" then
        print("YYYYYYYYYYYYYYYYYYYYYY");
    elseif event.name == "pageChange" then
        print("xxxxxxxxxxxxxxxxxxxx");
        self.PageViewMark:setSelectedIndex(event.pageIdx);
    end
end
-]]

function BackpackWnd:onClose()
	
end

function BackpackWnd:btnClicked()
    mgrs.log:printDebug("!!!!!BackpackWnd onClose!!!!!");
end



function BackpackWnd:reqData(handler)
	handler(true);
end

function BackpackWnd:getRelativePosition()
	return {rx = 0.5; ry = 0.5; ox = mgrs.wnd.visibleRect.center.x; oy = mgrs.wnd.visibleRect.center.y;};
end

function BackpackWnd:getLayer()
	return enums.Layer.Window;
end

function BackpackWnd:getPreloadResList()
	return {};
end

function BackpackWnd:getExclusiveType()
	return 1;
end

function BackpackWnd:isOpenCloseSoundEnabled()
	return false;
end

function BackpackWnd:onEnter()
	print("BackpackWnd:onEnter");
end
function BackpackWnd:onExit()
	print("BackpackWnd:onExit");
end
function BackpackWnd:onEnterTransitionFinish()
	print("BackpackWnd:onEnterTransitionFinish");
end

function BackpackWnd:onExitTransitionStart()
	print("BackpackWnd:onExitTransitionStart");
end




mgrs.wnd:registerWnd(enums.Wnd.Backpack,BackpackWnd);

--endregion
